package com.example.discover.view

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.material.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.discover.R
import com.example.discover.model.Book
import com.example.discover.viewmodel.BookViewModel
import com.example.discover.viewmodel.BookViewModelFactory
import androidx.compose.foundation.layout.R as R1

@Composable
fun BookScreen(bookViewModel: BookViewModel = viewModel(factory = BookViewModelFactory.instance)){

    val books by bookViewModel.books.observeAsState(listOf(Book("","","")))
    val totalSize by bookViewModel.totalSize.observeAsState("")
    val totalPrice by bookViewModel.totalPrice.observeAsState("")
    val book by bookViewModel.book.observeAsState(Book("","",""))

    Column() {

        BookSummary(totalSize,totalPrice)

        Spacer(Modifier.height(8.dp))

        BookList(books, bookViewModel)

        Spacer(Modifier.height(8.dp))

        BookForm(book, bookViewModel)
    }
}

@Composable
fun BookSummary(totalSize: String, totalPrice: String){
    Row(modifier = Modifier.padding(all = 8.dp)) {
        Text(text = "Total Count:")

        Spacer(modifier = Modifier.width(20.dp))

        Text(text = "$totalSize")

        Spacer(modifier = Modifier.width(60.dp))

        Text(text = "Total Price:")

        Spacer(modifier = Modifier.width(20.dp))

        Text(text = "$totalPrice")
    }
}

@Composable
fun BookForm(book: Book, bookViewModel: BookViewModel){

    Column() {

        Row (modifier = Modifier.padding(all = 8.dp)){

            Button(onClick = {
                bookViewModel.add(book)
            }) {
                Text("Add")
            }

            Spacer(modifier = Modifier.width(20.dp))

            Button(onClick = {
                bookViewModel.update(book)
            }) {
                Text("Update")
            }

            Spacer(modifier = Modifier.width(20.dp))

            Button(onClick = {
                bookViewModel.remove(book.isbn)
            }) {
                Text("Remove")
            }
        }

        BookTextField("ISBN",mutableStateOf(book.isbn), KeyboardType.Ascii, _onValueChange = {book.isbn = it })

        Spacer(modifier = Modifier.height(5.dp))

        BookTextField("Name",mutableStateOf(book.name), _onValueChange = {book.name = it })

        Spacer(modifier = Modifier.height(5.dp))

        BookTextField("Price",mutableStateOf(book.price), KeyboardType.Number, _onValueChange = {book.price = it })
    }
}

@Composable
fun BookList(books:List<Book>, bookViewModel: BookViewModel){

    Image(painter = painterResource(R.drawable.ic_refresh),
        contentDescription = "Click to refresh",
        contentScale = ContentScale.Crop,
        modifier = Modifier.clickable {
            bookViewModel.refresh()
        }
    )

    Text("Book List:")

    LazyColumn(Modifier.padding(8.dp, 16.dp, 0.dp, 0.dp)) {
        items(books) { book ->
            Text("${book.isbn}  ${book.name}  ${book.price}")
        }
    }
}


@Composable
fun BookTextField(label:String, fieldValue: MutableState<String>, keyboardType:KeyboardType = KeyboardType.Text,  _onValueChange: (String) -> Unit){

    Row {
        Text(
            text = label, textAlign = TextAlign.Left, modifier = Modifier
                .width(60.dp)
                .padding(8.dp, 16.dp, 0.dp, 0.dp)
        )

        Spacer(modifier = Modifier.width(10.dp))

        TextField(
            value = fieldValue.value,
            onValueChange = {
                fieldValue.value = it
                _onValueChange(it)
            },
            keyboardOptions = KeyboardOptions(keyboardType = keyboardType),
            colors = TextFieldDefaults.textFieldColors(
                backgroundColor = Color.White,
                cursorColor = Color.Black
            )
        )
    }
}